<?php

namespace App\Controller\Game;

use App\Base\BaseCache;
use App\Base\BaseController4App;
use App\Base\BaseController4MiniApp;
use App\Base\Format;
use App\Entity\SuanMingLog;
use App\Entity\User;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\UserModel;
use App\Model\SuanMingFenXiModel;
use App\Model\SuanMingLogModel;
use App\Model\SuanMingModel;
use App\Model\TopicModel;
use App\Model\UserLiveModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/game")
 */
class SuanMingController extends BaseController4MiniApp
{


    /**
     * @route("/suanming",name="GameSuanMing")
     */
    public function GameSuanMing()
    {
        $user  = $this->init();


        $mo = new SuanMingModel($this->getDoctrine());
        $SuanMingFenXiModel = new SuanMingLogModel($this->getDoctrine());
        $log = $SuanMingFenXiModel->getByKVs([
            "uid"=>$user->getId()
            ,"day"=>date(YMD)
        ]);
        if ($log) {
            $curSuanMing = $mo->getById($log->getSid());
        }else{
            $list = $mo->getAll();
            $max = count($list);
            $cur = rand(0,$max-1);
            $curSuanMing = $list[$cur];

            $SuanMingLog = new SuanMingLog();
            $SuanMingLog->setUid($user->getId())
                ->setSid($curSuanMing->getId())
                ->setAdddate(date($this->YYMMDDHHIISS))
                ->setTitle($curSuanMing->getTitle())
                ->setDay(date(YMD));
            $this->myEm()->persist($SuanMingLog);$this->myEm()->flush();
        }
        $id = $curSuanMing->getId();
        $curSuanMing = $this->toArray($curSuanMing);
        $SuanMingFenXiModel = new SuanMingFenXiModel($this->getDoctrine());
        $list = $SuanMingFenXiModel->getArrayByKV("suan_ming_id",$id);
        $curSuanMing["list"] = $list;
        return $this->json_out(1,"OK",$curSuanMing);

    }

    /**
     * @route("/suanminglog",name="GameSuanMingLog")
     */
    public function GameSuanMingLog()
    {
        $user  = $this->init();
        $SuanMingFenXiModel = new SuanMingLogModel($this->getDoctrine());
        $list = $SuanMingFenXiModel->getArrayByKV("uid",$user->getId());
        return $this->json_out(1,"OK",$list);

    }


    /**
     * @route("/suanminginfo",name="GameSuanMingInfo")
     */
    public function GameSuanMingInfo()
    {
        $user  = $this->init();
        if (!$id = $this->getParamRequired("id")) {return $this->json_out(0,"信息丢失");}

        $mo = new SuanMingModel($this->getDoctrine());
        $curSuanMing = $mo->getById($id);
        $id = $curSuanMing->getId();
        $curSuanMing = $this->toArray($curSuanMing);
        $SuanMingFenXiModel = new SuanMingFenXiModel($this->getDoctrine());
        $list = $SuanMingFenXiModel->getArrayByKV("suan_ming_id",$id);
        $curSuanMing["list"] = $list;
        return $this->json_out(1,"OK",$curSuanMing);

    }


}
